package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.thoughtworks.xstream.XStream;

public class AddressDataGenerator {
	
	public static void main(String[] args) throws IOException {
		if (args.length < 3){
			System.out.println("Please, specify  parameters: <amount of tests> <file> <format>");
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if(file.exists()){
			System.out.println("File exists, please remove it manually: " + file);
			return;
		}
		
		List <AddressData> addresses = generateRandomAddresses(amount);
		if ("csv".equals(format)){
			saveAddressesToCsvFile(addresses, file);
		}else if ("xml".equals(format)){
			saveAddressesToXmlFile(addresses, file);
		} else {
			System.out.println("Unknown format" + format);
			return;
		}
	}

	private static void saveAddressesToXmlFile(List<AddressData> addresses, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("address", AddressData.class);
		String xml = xstream.toXML(addresses);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}
	
	public static List<AddressData> loadAddressesFromXmlFile (File file) {
		XStream xstream = new XStream();
		xstream.alias("address", AddressData.class);
		return (List<AddressData>) xstream.fromXML(file);
	}

	private static void saveAddressesToCsvFile(List<AddressData> addresses, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (AddressData address : addresses) {
			writer.write(address.getFirstname() + "," + address.getLastname() + "," + address.getAddress() + "," + address.getHomephone()
					+ "," + address.getMobilephone() + "," + address.getWorkphone() + "," + address.getEmail() + "," + address.getEmail2() + ","
					+ address.getBday() + "," + address.getBmonth() + "," + address.getByear() + "," + address.getAddress2() + "," 
					+ address.getHomephone2() + ",!" +"\n");
		}
		writer.close();
		
	}
	

	 public static List<AddressData> loadAddressesFromCsvFile(File file) throws IOException {
		 	List<AddressData> list = new ArrayList<AddressData>();
			FileReader reader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader (reader);
			String line = bufferedReader.readLine();
			while (line != null){
				String[] part = line.split(",");
				AddressData address = new AddressData()
					.withFirstname(part[0])
					.withLastname(part[1])
					.withAddress(part[2])
					.withHomephone(part[3])
					.withMobilephone(part[4])
					.withWorkphone(part[5])
					.withEmail(part[6])
					.withEmail2(part[7])
					.withBday(part[8])
					.withBmonth(part[9])
					.withByear(part[10])
					.withAddress2(part[11])
					.withHomephone2(part[12]);
				list.add(address);
				line = bufferedReader.readLine();
			}
			
			bufferedReader.close();
			return list;
	}

	public static List<AddressData> generateRandomAddresses(int amount) {
		List<AddressData> list = new ArrayList<AddressData>();
		for (int i = 0; i < amount; i++){
			AddressData addressdata = new AddressData()
				.withFirstname(generateRandomString())
				.withLastname(generateRandomString())
				.withAddress(generateRandomString())
				.withHomephone(generateRandomString())
				.withMobilephone(generateRandomString())
				.withWorkphone(generateRandomString())
				.withEmail(generateRandomString())
				.withEmail2(generateRandomString())
				.withBday(generateBDay())
				.withBmonth(generateBMonth())
				.withByear(generateBYear())
				.withAddress2(generateRandomString())
				.withHomephone2(generateRandomString());
		
			list.add(addressdata);
		}
		return list;
	}
	
	public static String generateRandomString(){        // this method should be deleted
		Random rnd = new Random();
		if(rnd.nextInt(3) == 0){
			return "";
		} else {
			return "test" + rnd.nextInt();
		}
	}
	
	public static String generateBDay(){
		Random rnd = new Random();
		String string="";
		int bday = rnd.nextInt(31);
		if (bday == 0){
			return "-";
		} else {
			string = string + bday;
			return string;
		}
		
	}
	
	public static String generateBMonth(){
		Random rnd = new Random();
		int stringNumber = rnd.nextInt(12);
		String month = "";
		switch(stringNumber){
			case 0: month = "-"; break;
			case 1: month = "January"; break;
			case 2: month = "February"; break;
			case 3: month = "March"; break;
			case 4: month = "April"; break;
			case 5: month = "May"; break;
			case 6: month = "June"; break;
			case 7: month = "July"; break;
			case 8: month = "August"; break;
			case 9: month = "September"; break;
			case 10: month = "October"; break;
			case 11: month = "November"; break;
			case 12: month = "December"; break;
		}
		return month;
	}
	
	public static String generateBYear(){
		Random rnd = new Random();
		String string="";
		int byear = rnd.nextInt(2013);
		if (byear == 0){
			return "-";
		} else {
			string = string + byear;
			return string;
		}
		
	}

}
